package com.xb.cloud.usercenter.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xb.cloud.common.util.BeanUtils;
import com.xb.cloud.gateway.client.ReturnCodes;
import com.xb.cloud.gateway.client.ReturnUtils;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.product.merchant.service.client.ShopService;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;
import com.xb.cloud.user.service.client.EndUserFavoriteShopService;
import com.xb.cloud.user.service.client.bo.EndUserFavoriteShopBo;
import com.xb.cloud.usercenter.client.vo.FavoriteShopVo;
import com.xb.cloud.usercenter.service.UserFavoriteShopService;

@Service
public class UserFavoriteShopServiceImpl implements UserFavoriteShopService {

	@Resource
	EndUserFavoriteShopService endUserFavoriteShopService;

	@Resource
	ShopService shopService;

	@Override
	public RtnInfo addFavoriteShop(FavoriteShopVo favoriteShopVo) {
		EndUserFavoriteShopBo endUserFavoriteShopBo = BeanUtils.newAndCopyProperties(EndUserFavoriteShopBo.class,
				favoriteShopVo);
		int inserted = endUserFavoriteShopService.addFavoriteShop(endUserFavoriteShopBo);
		return ReturnUtils.success(inserted);
	}

	@Override
	public RtnInfo isFollowed(FavoriteShopVo favoriteShopVo) {
		EndUserFavoriteShopBo endUserFavoriteShopBo = BeanUtils.newAndCopyProperties(EndUserFavoriteShopBo.class,
				favoriteShopVo);
		Boolean followed = endUserFavoriteShopService.isFollowed(endUserFavoriteShopBo);
		return ReturnUtils.success(followed);
	}

	@Override
	public RtnInfo cancelFollow(FavoriteShopVo favoriteShopVo) {
		EndUserFavoriteShopBo endUserFavoriteShopBo = BeanUtils.newAndCopyProperties(EndUserFavoriteShopBo.class,
				favoriteShopVo);
		Boolean followed = endUserFavoriteShopService.cancelFollow(endUserFavoriteShopBo);
		return ReturnUtils.success(followed);
	}

	@Override
	public RtnInfo getFavoriteShopList(FavoriteShopVo favoriteShopVo) {
		EndUserFavoriteShopBo endUserFavoriteShopBo = BeanUtils.newAndCopyProperties(EndUserFavoriteShopBo.class,
				favoriteShopVo);
		// q:pageNum->startPage
		endUserFavoriteShopBo.setStartPage(favoriteShopVo.getPageNum());
		// 获取收藏店铺的id
		List<Long> favoriteShopsData = null;
		ServiceReturnInfo favoriteShops = endUserFavoriteShopService.getFavoriteShopList(endUserFavoriteShopBo);
		if (!ServiceReturnUtils.isSuccess(favoriteShops)) {
			return ReturnUtils.error(favoriteShops.getCode(), favoriteShops.getMessage(),
					favoriteShops.getDetailMessage());
		}
		// 取出id
		favoriteShopsData = (List<Long>) favoriteShops.getData();
		if (favoriteShopsData == null)
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		else if (favoriteShopsData.size() == 0) {
			// 如果长度为零，提前返回。
			return ReturnUtils.success(new Byte[0]);
		}

		// 根据id获取店铺信息
		ServiceReturnInfo shopBoList = shopService.getShopBoListByIds(favoriteShopsData);
		if (ServiceReturnUtils.isError(shopBoList)) {
			return ReturnUtils.error(shopBoList.getCode(), shopBoList.getMessage(), shopBoList.getDetailMessage());
		}
		return ReturnUtils.success(shopBoList.getData());
	}
}
